package org.bonorsoft.modules.education.mapper;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bonorsoft.modules.education.entity.Notice;
import org.bonorsoft.modules.education.vo.NoticeVo;
import org.bonorsoft.modules.education.vo.SendVo;

import java.util.List;

/**
 * @Description: 通知表
 * @Author: WilliamYe
 * @Date:   2021-06-22
 * @Version: V1.0
 */
public interface NoticeMapper extends BaseMapper<Notice> {

    @Select("select n.notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag, " +
            "addressee_id, addressee_name, addressee_read, read_time, sign_up, participants_name, " +
            "(select enclosure_route from sys_enclosure where notice_response_id = n.notice_id) as notice_enclosure_route " +
            "from sys_notice n ,sys_addressee a " +
            "${ew.customSqlSegment} and n.notice_id = a.notice_id " +
            "order by notice_sending_time desc ")
    IPage<NoticeVo> paging(Page<Notice> page, @Param(Constants.WRAPPER) QueryWrapper<Notice> queryWrapper);

    @Select("select notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag " +
            "from sys_notice ${ew.customSqlSegment} " +
            "order by notice_sending_time desc ")
    IPage<NoticeVo> listPaging(Page<Notice> page, @Param(Constants.WRAPPER) QueryWrapper<Notice> queryWrapper);

    @Select("select n.notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag, " +
            "addressee_id, addressee_name, addressee_read, read_time, sign_up, participants_name " +
            "from sys_notice n ,sys_addressee a " +
            "where n.notice_id = a.notice_id and addressee_id = #{addresseeId} " +
            "order by notice_sending_time desc ")
    Notice getByAddresseeId(String addresseeId);

    @Select("select notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag, " +
            "('已读【'||(select count(0) from sys_addressee where addressee_read = '1' and sys_addressee.notice_id = sys_notice.notice_id)||'】/'||(select count(0) from sys_addressee where sys_addressee.notice_id = sys_notice.notice_id )) as noticeRead " +
            "from sys_notice ${ew.customSqlSegment} " +
            "order by notice_sending_time desc ")
    IPage<SendVo> sendPaging(Page<Notice> page, @Param(Constants.WRAPPER) QueryWrapper<Notice> queryWrapper);

    @Select("select notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag, " +
            "('已读【'||(select count(0) from sys_addressee where addressee_read = '1' and sys_addressee.notice_id = sys_notice.notice_id)||'】/'||(select count(0) from sys_addressee where sys_addressee.notice_id = sys_notice.notice_id )) as noticeRead " +
            "from sys_notice where notice_sender = #{noticeSender} and notice_status = '1' and del_flag = '0' " +
            "order by notice_sending_time desc ")
    List<SendVo> mobileSendList(String noticeSender);

    @Select("select notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag " +
            "from sys_notice where notice_sender = #{noticeSender} and notice_status = '0' and del_flag = '0' " +
            "order by notice_sending_time desc ")
    List<NoticeVo> mobileDraftList(String noticeSender);

    @Select("select notice_id, notice_theme, notice_content, notice_sender, notice_signature, notice_sending_time, notice_type, notice_message, notice_message_status, notice_status, del_flag " +
            "from sys_notice where notice_sender = #{noticeSender} and del_flag = '1' " +
            "order by notice_sending_time desc ")
    List<NoticeVo> mobileDeleteList(String noticeSender);
}
